/**
 * @author Fang Lu, fanglupku@gmail.com
 * 2011-5-16
 */
package com.autotag.nlp;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.net.URL;

import mark.chunking.Chunker;

public class NPChunkDealer {
	private static Chunker chunker;
	private static HashMap<String, String> chunkTags;
	static {
		try {
			File ruleFile = new File("resources\\rules");
			chunker = new Chunker(new URL("file", "", ruleFile
					.getAbsolutePath()));
			BufferedReader in = new BufferedReader(new FileReader(
					"resources\\pos_tag_dict"));
			String line = in.readLine();
			chunkTags = new HashMap<String, String>();
			while (line != null) {
				if (!line.trim().equals("")) {
					String[] tags = line.split(" ");
					chunkTags.put(tags[0], tags[1]);
				}
				line = in.readLine();
			}
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("unchecked")
	public static ArrayList<String> getNounPhraseList(String taggedString, boolean isStemming){
		ArrayList<String> resultList = new ArrayList<String>();
		String[] tokens = taggedString.split(" ");
		List wl = new ArrayList<String>();
		List tl = new ArrayList<String>();
		List pl = new ArrayList<String>();
		for (int i = 0; i < tokens.length; ++i) {
			String[] data = tokens[i].split("/");
			if (data.length != 2){
				continue;
			}
			wl.add(data[0]);
			pl.add(data[1]);

			String ct = (String) chunkTags.get(data[1]);

			if (ct == null)
				ct = "I";

			tl.add(ct);
		}
		tl = chunker.chunkSentence(wl, tl, pl);
		boolean inBaseNP = false;
		boolean lineBegin = true;
		boolean isNP = false;
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < wl.size(); ++i) {
			String ct = (String) tl.get(i);

			if (inBaseNP) {
				if (ct.equals("B")) {
//					System.out.print(" ] [");
					resultList.add(sb.toString().trim());
					sb.delete(0, sb.length());
					isNP = true;
				} else if (ct.equals("O")) {
//					System.out.print(" ]");
					inBaseNP = false;
					resultList.add(sb.toString().trim());
					sb.delete(0, sb.length());
					isNP = false;
				}
			} else {
				if (ct.equals("B") || ct.equals("I")) {
					if (!lineBegin)
//						System.out.print(" ");
					lineBegin = false;
//					System.out.print("[");
					inBaseNP = true;
					isNP = true;
				}
			}
			if (!lineBegin)
//				System.out.print(" ");
			lineBegin = false;
//			System.out.print(wl.get(i) + "/" + pl.get(i));
			if (isNP == true){
				if (isStemming){
					sb.append(WordStemmer.getStem(((String)wl.get(i)).toLowerCase()));
					sb.append(" ");
				}else{
					sb.append(wl.get(i) + " ");
				}	
			}
		}

		if (inBaseNP) {
//			System.out.print("]");
			resultList.add(sb.toString().trim());
		}
//		System.out.println();
		return resultList;
	}
	
	@SuppressWarnings("unchecked")
	public static void printNPBorderedString(String taggedString) {
		String[] tokens = taggedString.split(" ");
		List wl = new ArrayList<String>();
		List tl = new ArrayList<String>();
		List pl = new ArrayList<String>();
		for (int i = 0; i < tokens.length; ++i) {
			String[] data = tokens[i].split("/");

			wl.add(data[0]);
			pl.add(data[1]);

			String ct = (String) chunkTags.get(data[1]);

			if (ct == null)
				ct = "I";

			tl.add(ct);
		}
		tl = chunker.chunkSentence(wl, tl, pl);
		boolean inBaseNP = false;
		boolean lineBegin = true;
		for (int i = 0; i < wl.size(); ++i) {
			String ct = (String) tl.get(i);

			if (inBaseNP) {
				if (ct.equals("B")) {
					System.out.print(" ] [");
				} else if (ct.equals("O")) {
					System.out.print(" ]");
					inBaseNP = false;
				}
			} else {
				if (ct.equals("B") || ct.equals("I")) {
					if (!lineBegin)
						System.out.print(" ");
					lineBegin = false;
					System.out.print("[");
					inBaseNP = true;
				}
			}
			if (!lineBegin)
				System.out.print(" ");
			lineBegin = false;
			System.out.print(wl.get(i) + "/" + pl.get(i));
		}

		if (inBaseNP) {
			System.out.print("]");
		}
		System.out.println();
	}

	public static void main(String args[]) {
		String src = "Get Cities By Country";
		String taggedString = AnalyzeResultOfTagging.taggerString(src);
//		printNPBorderedString(taggedString);
		ArrayList<String> tList = getNounPhraseList(taggedString, true);
//		System.out.println(tList.size());
		for (String string : tList) {
			System.out.println(string);
		}
	}
}
